﻿using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using webnew.Common;  //引用公共类命名空间
using webnew.UserControls;
using WebPageBase;    //引用页面基类命名空间
namespace webnew.Systems
{

    public partial class S_compEdit : EditBase
    {
     
        protected void Page_Load(object sender, EventArgs e)
        {
            _ListSrc = "S_complist.aspx";            //返回的页面名
            if (!IsPostBack)
            {
                SetTitle(true, "后台管理>>单位管理");         //设置标题        
                webnew.Common.CommonHelper.DataDdlBind(ddlCompLevel, new BLL.Sys_SystemType().GetconList("系统单位级别"), "TypeName", "TypeName");

            }
      
        }


        /// <summary>
        /// 单位级别选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlCompLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCompLevel.SelectedValue != "一级单位")
            {
                if (ddlCompLevel.SelectedValue == "二级单位")
                    Common.CommonHelper.DataDdlBind(ddlParentID, new BLL.Sys_Company().GetLevelCompany("一级单位"), "compname", "id");
                else
                    Common.CommonHelper.DataDdlBind(ddlParentID, new BLL.Sys_Company().GetLevelCompany("二级单位"), "compname", "id");
                pnlParentId.Visible = true;

            }
            else
            {
                pnlParentId.Visible = false;
            }
        }


        /// <summary>
        /// 根据单位的ID显示数据
        /// </summary>
        protected override void ShowData()
        {
            Model.Sys_CompanyInfo model = new BLL.Sys_Company().GetList(_RecordId);
            ddlCompLevel.SelectedValue = model.CompanyLevel.ToString(); //单级级别
            ddlCompLevel_SelectedIndexChanged(null, null);
            ddlParentID.SelectedValue = model.ParentID;  //父ID
            txtSort.Text = MyConvert.toString(model.sort);                  //序号       
            txtCompName.Text = model.compname;                             //单位名称
            txtCompAbb.Text = model.compAbb;        //公司简称
            txtERPCode.Text = model.ERPCode;        //erp代码
            txtcomptype.Text= model.comptype;//物理代码
            //rblSetType.SelectedValue = model.SetType;  //设置类型
            
            //if (rblSetType.SelectedValue == "HR设置")
            //{
            //    ddlOaComp.SelectedValue = model.OASetId;   //对应OA单位
            //}
            //else
            //{ 
            //    UcCompType1.SelectedValue=model.comptype;  //单位类型
            //}   
            ViewState["currentCompName"] = model.compname; //保存当前公司名,用于验证判断     
            ViewState["currentCompERPCode"] = model.ERPCode; //保存当前ERP代码,用于验证判断         
            ddlisdel.SelectedValue =MyConvert.toString(model.isdel);   //状态
            txtRemark.Text = model.remark; //备注
        }

        /// <summary>
        /// 重写基类，传入要验证的数据
        /// </summary>
        protected override List<PageValid> _SetValidData()
        {
            List<PageValid> lst = new List<PageValid>();
            lst.Add(new PageValid("序号", txtSort.Text.Replace(" ", ""), PageValid.validType.CommonValid, false, 5));
            lst.Add(new PageValid("序号", txtSort.Text.Replace(" ", ""), PageValid.validType.IntValid, "只能为正整数"));
            lst.Add(new PageValid("序号", "0", txtSort.Text.Replace(" ", ""), PageValid.validType.NoSameValid, "不能为0数字"));
            lst.Add(new PageValid("公司单位", this.txtCompName.Text, PageValid.validType.CommonValid, false, 25));
            lst.Add(new PageValid("单位简称", this.txtCompAbb.Text, PageValid.validType.CommonValid, true, 25));
            lst.Add(new PageValid("ERP代码", this.txtERPCode.Text, PageValid.validType.CommonValid, true, 25));
            lst.Add(new PageValid("备注", this.txtRemark.Text, PageValid.validType.CommonValid, true, 500));
            ////判断是否存在相同的单位名
            if (_RecordId != null) //修改
            {
                //判断用户在修改状态下是否修改过单位名称,没修改过 则跳过验证
                if (ViewState["currentCompName"].ToString() != txtCompName.Text)
                    lst.Add(new PageValid("公司单位", "1", new BLL.Sys_Company().ExistCompName(txtCompName.Text,ddlCompLevel.SelectedValue), PageValid.validType.NoSameValid, "已存在,请重新输入"));
            }
            else //添加
                lst.Add(new PageValid("公司单位", "1", new BLL.Sys_Company().ExistCompName(txtCompName.Text, ddlCompLevel.SelectedValue), PageValid.validType.NoSameValid, "已存在,请重新输入"));
            
            ////判断是否存在相同的ERP代码
            if (_RecordId != null) //修改
            {
                //判断用户在修改状态下是否修改过ERP代码,没修改过 则跳过验证
                if (ViewState["currentCompERPCode"].ToString() != txtCompName.Text)
                    lst.Add(new PageValid("ERP代码", "1", new BLL.Sys_Company().ExistERPCode(txtERPCode.Text, ddlCompLevel.SelectedValue), PageValid.validType.NoSameValid, "已存在,请重新输入"));
            }
            else //添加
                lst.Add(new PageValid("ERP代码", "1", new BLL.Sys_Company().ExistERPCode(txtERPCode.Text, ddlCompLevel.SelectedValue), PageValid.validType.NoSameValid, "已存在,请重新输入"));
            return lst;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns>成功则返回1</returns>
        protected override int SaveData()
        {
            Model.Sys_CompanyInfo model = new Model.Sys_CompanyInfo();
            model.Id = _RecordId;
            model.CompanyLevel = ddlCompLevel.SelectedValue;
            if (ddlCompLevel.SelectedValue == "一级单位")
            {
                 model.ParentID = Guid.Empty.ToString();
            }
            else
            {
               model.ParentID = ddlParentID.SelectedValue;
            }
            model.compname = txtCompName.Text;
            model.compAbb = txtCompAbb.Text;
            model.ERPCode = txtERPCode.Text;
            model.sort = MyConvert.toInt32(txtSort.Text);
            model.comptype = txtcomptype.Text;//物理代码
            //model.SetType = rblSetType.SelectedValue;
            //if (rblSetType.SelectedValue == "OA设置")
            //{
            //    model.comptype = UcCompType1.SelectedValue;
            //    model.OASetId = Guid.Empty.ToString();
            //}
            //else
            //{
            //    model.comptype = "";
            //    model.OASetId = ddlOaComp.SelectedValue;
            //}
                       
            model.isdel = MyConvert.toInt32(ddlisdel.SelectedValue);
            model.remark = txtRemark.Text;
         

            //----------系统日志model

            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息


            //修改
            if (_RecordId != null)
            {


                BLL.Sys_Company bll = new webnew.BLL.Sys_Company();
                try
                {
                    return bll.Update(model);
                }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "修改", modelLog, "系统单位", "单位，单位名称为" + txtCompName.Text);
                }
            }

            //添加
            else
            {

                BLL.Sys_Company bll = new webnew.BLL.Sys_Company();
                try { return bll.Add(model); }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "添加", modelLog, "系统单位", "单位，单位名称为" + txtCompName.Text);
                }
            }

            //弹出文本框成功或者失败
            if (error == "") { return 1; }       //成功
            else { return 0; }                   //失败
           
        }

      
    }
}
